{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.16 \u4e0d\u786e\u5b9a\u7f16\u7801\u683c\u5f0f\u7684C\u5b57\u7b26\u4e32\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8981\u5728C\u548cPython\u76f4\u63a5\u6765\u56de\u8f6c\u6362\u5b57\u7b26\u4e32\uff0c\u4f46\u662fC\u4e2d\u7684\u7f16\u7801\u683c\u5f0f\u5e76\u4e0d\u786e\u5b9a\u3002\n\u4f8b\u5982\uff0c\u53ef\u80fdC\u4e2d\u7684\u6570\u636e\u671f\u671b\u662fUTF-8\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u5f3a\u5236\u5b83\u5fc5\u987b\u662f\u3002\n\u4f60\u60f3\u7f16\u5199\u4ee3\u7801\u6765\u4ee5\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u5904\u7406\u8fd9\u4e9b\u4e0d\u5408\u683c\u6570\u636e\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u8ba9Python\u5954\u6e83\u6216\u8005\u7834\u574f\u8fdb\u7a0b\u4e2d\u7684\u5b57\u7b26\u4e32\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e9bC\u7684\u6570\u636e\u548c\u4e00\u4e2a\u51fd\u6570\u6765\u6f14\u793a\u8fd9\u4e2a\u95ee\u9898\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* Some dubious string data (malformed UTF-8) */\nconst char *sdata = \"Spicy Jalape\\xc3\\xb1o\\xae\";\nint slen = 16;\n\n/* Output character data */\nvoid print_chars(char *s, int len) {\n  int n = 0;\n  while (n < len) {\n    printf(\"%2x \", (unsigned char) s[n]);\n    n++;\n  }\n  printf(\"\\n\");\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u5b57\u7b26\u4e32 sdata \u5305\u542b\u4e86UTF-8\u548c\u4e0d\u5408\u683c\u6570\u636e\u3002\n\u4e0d\u8fc7\uff0c\u5982\u679c\u7528\u6237\u5728C\u4e2d\u8c03\u7528 print_chars(sdata, slen) \uff0c\u5b83\u7f3a\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\n\u73b0\u5728\u5047\u8bbe\u4f60\u60f3\u5c06 sdata \u7684\u5185\u5bb9\u8f6c\u6362\u4e3a\u4e00\u4e2aPython\u5b57\u7b26\u4e32\u3002\n\u8fdb\u4e00\u6b65\u5047\u8bbe\u4f60\u5728\u540e\u9762\u8fd8\u60f3\u901a\u8fc7\u4e00\u4e2a\u6269\u5c55\u5c06\u90a3\u4e2a\u5b57\u7b26\u4e32\u4f20\u4e2a print_chars() \u51fd\u6570\u3002\n\u4e0b\u9762\u662f\u4e00\u79cd\u7528\u6765\u4fdd\u62a4\u539f\u59cb\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u5c31\u7b97\u5b83\u7f16\u7801\u6709\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* Return the C string back to Python */\nstatic PyObject *py_retstr(PyObject *self, PyObject *args) {\n  if (!PyArg_ParseTuple(args, \"\")) {\n    return NULL;\n  }\n  return PyUnicode_Decode(sdata, slen, \"utf-8\", \"surrogateescape\");\n}\n\n/* Wrapper for the print_chars() function */\nstatic PyObject *py_print_chars(PyObject *self, PyObject *args) {\n  PyObject *obj, *bytes;\n  char *s = 0;\n  Py_ssize_t   len;\n\n  if (!PyArg_ParseTuple(args, \"U\", &obj)) {\n    return NULL;\n  }\n\n  if ((bytes = PyUnicode_AsEncodedString(obj,\"utf-8\",\"surrogateescape\"))\n        == NULL) {\n    return NULL;\n  }\n  PyBytes_AsStringAndSize(bytes, &s, &len);\n  print_chars(s, len);\n  Py_DECREF(bytes);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5728Python\u4e2d\u5c1d\u8bd5\u8fd9\u4e9b\u51fd\u6570\uff0c\u4e0b\u9762\u662f\u8fd0\u884c\u6548\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = retstr()\ns"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(s)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ed4\u7ec6\u89c2\u5bdf\u7ed3\u679c\u4f60\u4f1a\u53d1\u73b0\uff0c\u4e0d\u5408\u683c\u5b57\u7b26\u4e32\u88ab\u7f16\u7801\u5230\u4e00\u4e2aPython\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e76\u4e14\u5e76\u6ca1\u6709\u4ea7\u751f\u9519\u8bef\uff0c\n\u5e76\u4e14\u5f53\u5b83\u88ab\u56de\u4f20\u7ed9C\u7684\u65f6\u5019\uff0c\u88ab\u8f6c\u6362\u4e3a\u548c\u4e4b\u524d\u539f\u59cbC\u5b57\u7b26\u4e32\u4e00\u6837\u7684\u5b57\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u5c55\u793a\u4e86\u5728\u6269\u5c55\u6a21\u5757\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u65f6\u4f1a\u914d\u5230\u7684\u4e00\u4e2a\u68d8\u624b\u53c8\u5f88\u607c\u706b\u7684\u95ee\u9898\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u6269\u5c55\u4e2d\u7684C\u5b57\u7b26\u4e32\u53ef\u80fd\u4e0d\u4f1a\u4e25\u683c\u9075\u5faaPython\u6240\u671f\u671b\u7684Unicode\u7f16\u7801/\u89e3\u7801\u89c4\u5219\u3002\n\u56e0\u6b64\uff0c\u5f88\u53ef\u80fd\u4e00\u4e9b\u4e0d\u5408\u683cC\u6570\u636e\u4f20\u9012\u5230Python\u4e2d\u53bb\u3002\n\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u5c31\u662f\u6d89\u53ca\u5230\u5e95\u5c42\u7cfb\u7edf\u8c03\u7528\u6bd4\u5982\u6587\u4ef6\u540d\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u8fd4\u56de\u7ed9\u89e3\u91ca\u5668\u4e00\u4e2a\u635f\u574f\u7684\u5b57\u7b26\u4e32\uff0c\u4e0d\u80fd\u88ab\u6b63\u786e\u89e3\u7801\u7684\u65f6\u5019\u4f1a\u600e\u6837\u5462\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u822c\u6765\u8bb2\uff0c\u53ef\u4ee5\u901a\u8fc7\u5236\u5b9a\u4e00\u4e9b\u9519\u8bef\u7b56\u7565\u6bd4\u5982\u4e25\u683c\u3001\u5ffd\u7565\u3001\u66ff\u4ee3\u6216\u5176\u4ed6\u7c7b\u4f3c\u7684\u6765\u5904\u7406Unicode\u9519\u8bef\u3002\n\u4e0d\u8fc7\uff0c\u8fd9\u4e9b\u7b56\u7565\u7684\u4e00\u4e2a\u7f3a\u70b9\u662f\u5b83\u4eec\u6c38\u4e45\u6027\u7834\u574f\u4e86\u539f\u59cb\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f8b\u5b50\u4e2d\u7684\u4e0d\u5408\u683c\u6570\u636e\u4f7f\u7528\u8fd9\u4e9b\u7b56\u7565\u4e4b\u4e00\u89e3\u7801\uff0c\u4f60\u4f1a\u5f97\u5230\u4e0b\u9762\u8fd9\u6837\u7684\u7ed3\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "raw = b'Spicy Jalape\\xc3\\xb1o\\xae'\nraw.decode('utf-8','ignore')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "raw.decode('utf-8','replace')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "surrogateescape \u9519\u8bef\u5904\u7406\u7b56\u7565\u4f1a\u5c06\u6240\u6709\u4e0d\u53ef\u89e3\u7801\u5b57\u8282\u8f6c\u5316\u4e3a\u4e00\u4e2a\u4ee3\u7406\u5bf9\u7684\u4f4e\u4f4d\u5b57\u8282\uff08udcXX\u4e2dXX\u662f\u539f\u59cb\u5b57\u8282\u503c\uff09\u3002\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "raw.decode('utf-8','surrogateescape')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5355\u72ec\u7684\u4f4e\u4f4d\u4ee3\u7406\u5b57\u7b26\u6bd4\u5982 \\udcae \u5728Unicode\u4e2d\u662f\u975e\u6cd5\u7684\u3002\n\u56e0\u6b64\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5c31\u662f\u4e00\u4e2a\u975e\u6cd5\u8868\u793a\u3002\n\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u4f60\u5c06\u5b83\u4f20\u4e2a\u4e00\u4e2a\u6267\u884c\u8f93\u51fa\u7684\u51fd\u6570\uff0c\u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u9519\u8bef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = raw.decode('utf-8', 'surrogateescape')\nprint(s)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u800c\uff0c\u5141\u8bb8\u4ee3\u7406\u8f6c\u6362\u7684\u5173\u952e\u70b9\u5728\u4e8e\u4eceC\u4f20\u7ed9Python\u53c8\u56de\u4f20\u7ed9C\u7684\u4e0d\u5408\u683c\u5b57\u7b26\u4e32\u4e0d\u4f1a\u6709\u4efb\u4f55\u6570\u636e\u4e22\u5931\u3002\n\u5f53\u8fd9\u4e2a\u5b57\u7b26\u4e32\u518d\u6b21\u4f7f\u7528 surrogateescape \u7f16\u7801\u65f6\uff0c\u4ee3\u7406\u5b57\u7b26\u4f1a\u8f6c\u6362\u56de\u539f\u59cb\u5b57\u8282\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.encode('utf-8','surrogateescape')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u4e00\u822c\u51c6\u5219\uff0c\u6700\u597d\u907f\u514d\u4ee3\u7406\u7f16\u7801\u2014\u2014\u5982\u679c\u4f60\u6b63\u786e\u7684\u4f7f\u7528\u4e86\u7f16\u7801\uff0c\u90a3\u4e48\u4f60\u7684\u4ee3\u7801\u5c31\u503c\u5f97\u4fe1\u8d56\u3002\n\u4e0d\u8fc7\uff0c\u6709\u65f6\u5019\u786e\u5b9e\u4f1a\u51fa\u73b0\u4f60\u5e76\u4e0d\u80fd\u63a7\u5236\u6570\u636e\u7f16\u7801\u5e76\u4e14\u4f60\u53c8\u4e0d\u80fd\u5ffd\u7565\u6216\u66ff\u6362\u574f\u6570\u636e\uff0c\u56e0\u4e3a\u5176\u4ed6\u51fd\u6570\u53ef\u80fd\u4f1a\u7528\u5230\u5b83\u3002\n\u90a3\u4e48\u5c31\u53ef\u4ee5\u4f7f\u7528\u672c\u8282\u7684\u6280\u672f\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u662f\uff0cPython\u4e2d\u8bb8\u591a\u9762\u5411\u7cfb\u7edf\u7684\u51fd\u6570\uff0c\u7279\u522b\u662f\u548c\u6587\u4ef6\u540d\u3001\u73af\u5883\u53d8\u91cf\u548c\u547d\u4ee4\u884c\u53c2\u6570\u76f8\u5173\u7684\n\u90fd\u4f1a\u4f7f\u7528\u4ee3\u7406\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u50cf os.listdir() \u8fd9\u6837\u7684\u51fd\u6570\uff0c\n\u4f20\u5165\u4e00\u4e2a\u5305\u542b\u4e86\u4e0d\u53ef\u89e3\u7801\u6587\u4ef6\u540d\u7684\u76ee\u5f55\u7684\u8bdd\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4ee3\u7406\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u3002\n\u53c2\u80035.15\u7684\u76f8\u5173\u7ae0\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "PEP 383\n\u4e2d\u6709\u66f4\u591a\u5173\u4e8e\u672c\u673a\u63d0\u5230\u7684\u4ee5\u53ca\u548csurrogateescape\u9519\u8bef\u5904\u7406\u76f8\u5173\u7684\u4fe1\u606f\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}